<template>
  <j-modal
    :title="title"
    :width="width"
    :visible="visible"
    switchFullscreen
    @ok="handleOk"
    :okButtonProps="{ class:{'jee-hidden': disableSubmit} }"
    @cancel="handleCancel"
    cancelText="关闭">

    <j-form-container>
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="巡检情况" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="situation">
              <a-textarea v-model="model.situation" rows="4" placeholder="请输入巡检情况" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="业主反馈" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="feedback">
              <a-textarea v-model="model.feedback" rows="4" placeholder="请输入业主反馈" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="巡检时间" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="circuitTime">
              <j-date placeholder="请选择巡检时间" v-model="model.circuitTime"  style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="巡检图片" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="circuitImageList">
              <j-upload v-model="model.circuitImageList" text="上传" fileType="image"></j-upload>
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>

  </j-modal>
</template>

<script>

  import { httpAction, getAction,getFileAccessHttpUrl } from '@/api/manage'
  import {ACCESS_TOKEN} from "@/store/mutation-types"
  import Vue from "vue";
  export default {
    name: 'GhSiteCircuitModal',
    components: {

    },
    data () {
      return {
        title:'',
        width:'100%',
        visible: false,
        disableSubmit: false,
        customerId: '',
        //circuitImageList: [],
        model:{

        },
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
          situation: [{ required: true, message: '巡检情况不能为空' }],
          circuitTime: [{ required: true, message: '巡检时间不能为空' }],
          circuitImageList: [{ required: true, message: '请上传巡检图片' },{validator: this.validateImage}],
        },
        url: {
          add: "/ghSiteCircuit/add",
          edit: "/ghSiteCircuit/edit",
          queryById: "/ghSiteCircuit/queryById"
        }
      }
    },
    created () {
      //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
    },
    methods: {
      validateImage(rule, value, callback){
        if(!value){
          callback()
        }else{
          if (this.model.circuitImageList == null){
            callback("请上传巡检图片!");
          }else {
            callback()
          }
        }
      },
      close () {
        this.$emit('close');
        this.visible = false;
        this.model.circuitImageList = [];
      },
      handleOk () {
        const that = this;
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
              method = 'put';
            }
            if (this.model.circuitImageList.length > 0){
              this.model.circuitImg = this.model.circuitImageList.toString();
            }else {
              this.model.circuitImg = "";
            }
            httpAction(httpurl,this.model,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
              this.close();
            })
          }
        })
      },
      handleCancel () {
        this.close()
      },
      add (customerId) {
        this.modelDefault.customerId = customerId;
        this.edit(this.modelDefault);
      },
      edit (record) {
        this.model = Object.assign({}, record);
        if (record.images != null && record.images.length !== 0){
          this.model.circuitImageList = [...record.images]
        }
        this.visible = true;
      },
    }
  }
</script>